﻿using System;
using System.Linq;
using System.Net;
using System.Threading;
using System.Diagnostics;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.Web.Administration;

namespace Bowlus.Common
{
    // static Helper methods and global used/shared object like ServerManager
    public class BowlusHelper : IDisposable
    {
        private static readonly object lockObject = new object();

        private static BowlusHelper helperInstance;

        // get this lock, before using ServerManager to do write operation
        public static readonly object ServerManagerLock = new object();

        private static void initHelper()
        {
            if (helperInstance == null)
            {
                // initiate helper, and other resources like ServerManager
                lock (lockObject)
                {
                    if (helperInstance == null)
                    {
                        helperInstance = new BowlusHelper();
                        if (helperInstance.serverManagerInstance == null)
                        {
                            helperInstance.serverManagerInstance = new ServerManager();
                        }
                    }
                }
            }
        }

        public static ServerManager ServerManager
        {
            get
            {
                initHelper();
                return BowlusHelper.helperInstance.serverManagerInstance;
            }
        }

        // get IPv4 address of the current server
        public static string GetHostIpAddress()
        {
            if (RoleEnvironment.IsEmulated)
            {
                var ip = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Http"].IPEndpoint.Address;
                // ip = "127.255.0.0" 
                return ip.ToString();
            }

            return Dns.GetHostAddresses(Dns.GetHostName())
                .First(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString();
        }

        public static void ConfigureLog()
        {
            var config = DiagnosticMonitor.GetDefaultInitialConfiguration();

            CrashDumps.EnableCollection(true);

            // For windows azure infrastructure itself
            config.DiagnosticInfrastructureLogs.ScheduledTransferPeriod = TimeSpan.FromMinutes(3);
            config.DiagnosticInfrastructureLogs.ScheduledTransferLogLevelFilter = LogLevel.Warning;

            // For windows azure logs. 1 minute is the minimun value we can set
            config.Logs.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);
            config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Information;

            DiagnosticMonitor.Start(BowlusSettings.DiagnosticStorageAccount, config);

            Trace.Listeners.Add(new DiagnosticMonitorTraceListener());

        }

        // non-static members

        private BowlusHelper()
        {
        }

        private ServerManager serverManagerInstance;

        // Dispose ServerManager
        public void Dispose()
        {
            if (serverManagerInstance != null)
            {
                serverManagerInstance.Dispose();
            }
        }

    }
}
